package br.ufrj.ppgi.jemf_advanced_example.commander;

import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ScrollView;
import br.ufrj.ppgi.jemf.mobile.bean.Commander;
import br.ufrj.ppgi.jemf.mobile.view.CommanderView;
import br.ufrj.ppgi.jemf.utils.CustomConversions;
import br.ufrj.ppgi.jemf.utils.CustomFormat;
import br.ufrj.ppgi.jemf_advanced_example.BuildConfig;

/**
 * A fragment representing a single Commander detail screen.
 * This fragment is either contained in a {@link CommanderListActivity} in two-pane mode (on tablets) or a {@link CommanderDetailActivity} on handsets.
 */
public class CommanderDetailFragment extends Fragment {
    /**
     * The serialization (saved instance state) Bundle key representing the activated item position.
     * Only used on tablets.
     */
    private static final String STATE_COMMANDER = "activated_commander";
	/**
     * The fragment argument representing the item ID that this fragment represents.
     */
    public static final String ARG_ID = "COMMANDER";
    /**
     * The Commander content this fragment is showing.
     */
    private static Commander commander;
    /**
     * The Commander view holder.
     */
    private static CommanderView commanderView;



    /**
     * Mandatory empty constructor for the fragment manager to instantiate the fragment (e.g. upon screen orientation changes).
     */
    public CommanderDetailFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Load the Commander content specified by the fragment arguments.
        if (getArguments().containsKey(CommanderDetailFragment.ARG_ID)) {
        	commander = getArguments().getParcelable(CommanderDetailFragment.ARG_ID);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
	    // Show the Commander content
	    if (commander != null) {
	    	commanderView = new CommanderView(getActivity(), false);
	    	// Name
	    	commanderView.setCommanderNameText(commander.getName());
	    	// Gender
	    	commanderView.setCommanderGenderBool(CustomFormat.IntToBool(commander.getGender()));
	    	// Birth Date
	    	commanderView.setCommanderBirthDateText(commander.getBirthDateString());
	    	// Age
	    	commanderView.setCommanderAgeText(String.valueOf(commander.getAge()));
	    	// Login
	    	commanderView.setCommanderLoginText(commander.getLogin());
	    	// Password
	    	commanderView.setCommanderPasswordText(commander.getPassword());

	    	// Its necessary to add the CommanderView inside a ScrollView to show scrolls
	    	ScrollView scrollView = new ScrollView(getActivity());
	    	scrollView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
	    	// Add Commander View as a child of the ScrollView.
	    	scrollView.addView(commanderView);
	    	// Add a Tag to the view location
	    	scrollView.setTag(ARG_ID);

			return scrollView;
    	} else {
        	return null;
    	}
    }

    @Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
        // Restore the previously serialized activated Commander
        if (savedInstanceState != null && savedInstanceState.containsKey(STATE_COMMANDER)) {
        	commander = savedInstanceState.getParcelable(STATE_COMMANDER);
        }
	}





	/**
     * Get updated Commander
     * @return Commander
     */
	public static Commander getCommander() {
		if (BuildConfig.DEBUG) Log.i(CommanderListActivity.TAG, "CommanderDetailFragment ::: getCommander");
    	return commander;
    }

	/**
	 * Update Commander data from UI components
	 */
	public static void updateDataFromUI() {
    	if ((commander != null) && (commanderView != null)) {
    		if (BuildConfig.DEBUG) Log.i(CommanderListActivity.TAG, "CommanderDetailFragment ::: updateDataFromUI");
    		// Update Commander object with new data included by user through components
    		commander.setName(commanderView.getCommanderNameText());
    		commander.setGender(CustomFormat.BoolToInt(commanderView.getCommanderGenderBool()));
    		commander.setBirthDateString(commanderView.getCommanderBirthDateText());
    		if (commanderView.getCommanderAgeText().isEmpty()) {
    			commander.setAge(0);
    		} else {
        		commander.setAge(CustomConversions.birthDateToAge(commander.getBirthDate()));
    	    	// Updating Age TO UI.
    	    	commanderView.setCommanderAgeText(String.valueOf(commander.getAge()));
    		}
    		commander.setLogin(commanderView.getCommanderLoginText());
    		commander.setPassword(commanderView.getCommanderPasswordText());
        }
    }



    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        // Force update data from UI
        updateDataFromUI();
        // Serialize and persist the activated Commander.
        outState.putParcelable(STATE_COMMANDER, commander);
    }
}
